package exam.microsoft.second.task1;

class Solution {
    public int solution(String S) {
        int[] chars = new int[S.length()];
        for (int i=0; i<S.length(); ++i)
            chars[i] = S.charAt(i) - 'a';
        return solution(chars, 0, S.length());
    }

    private int solution(int[] chars, int start, int end) {
        if (end - start < 2)
            return 0;
        int[] cnt = new int[26];
        for (int i=start; i<end; ++i)
            cnt[chars[i]]++;
        int i = start;
        while (i < end && cnt[chars[i]] % 2 == 0)
            ++i;
        if (i == end)
            return end - start;
        int left = solution(chars, start, i);
        while (i < end && cnt[chars[i]] % 2 == 1)
            ++i;
        int right = solution(chars, i, end);
        return Math.max(left, right);
    }
}
